package com.example.enterprisemanager.mvvm.fragment.manager;

import android.app.Application;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.enterprisemanager.base.BaseViewModel;
import com.example.enterprisemanager.mvvm.adapter.WaitPurchaseAdapter;
import com.example.enterprisemanager.mvvm.ben.User;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.SendPurchase;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.WaitPurchase;
import com.example.enterprisemanager.util.ToastUtil;
import com.orhanobut.logger.Logger;

import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BatchResult;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class WaitPurchaseViewModel extends BaseViewModel<WaitPurchaseModel> {
    public WaitPurchaseAdapter adapter;
    public MutableLiveData<SendPurchase> sendData;

    public WaitPurchaseViewModel(@NonNull Application application) {
        super(application, new WaitPurchaseModel());
        init();
    }

    private void init() {
        sendData = new MutableLiveData<>();

        adapter = new WaitPurchaseAdapter();

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Logger.d("onItemChildClick" + position);
                if (!BmobUser.isLogin() || BmobUser.getCurrentUser(User.class).getPower() > 4) {
                    ToastUtil.show(getApplication(), "您的权限不足，需要的权限为4及以上");
                    return;
                }

                WaitPurchase purchase = (WaitPurchase) adapter.getItem(position);
                sendData.setValue(new SendPurchase(position, purchase));
            }
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();

//        mModel.insertGoods(new Consumer<List<BatchResult>>() {
//            @Override
//            public void accept(List<BatchResult> batchResults) throws Exception {
//                ToastUtil.show(getApplication(), batchResults.size() + "条数据添加成功");
//
//            }
//        }, new Consumer<Exception>() {
//            @Override
//            public void accept(Exception e) throws Exception {
//                ToastUtil.show(getApplication(), "数据添加失败");
//            }
//        });

        mModel.getShopping(new Consumer<List<WaitPurchase>>() {
            @Override
            public void accept(List<WaitPurchase> shoppingGoods) throws Exception {
                adapter.setNewData(shoppingGoods);
            }
        }, new Consumer<Exception>() {
            @Override
            public void accept(Exception e) throws Exception {
                ToastUtil.show(getApplication(), "查询失败" + e.getMessage());
            }
        });
    }

    public void afterPurchase(WaitPurchase purchase, int num, int position) {
        if (num <= 0) {
            ToastUtil.show(getApplication(), "输入数据有误");
        } else if (num > purchase.getNumber()) {
            ToastUtil.show(getApplication(), "超过最大可购买数量");
        } else {
            int aNum = purchase.getNumber() - num;
            purchase.setNumber(aNum);
            purchase.setHaveSell(purchase.getHaveSell() + num);

            mModel.purchaseGoods(purchase, num, new Action() {
                @Override
                public void run() throws Exception {
                    ToastUtil.show(getApplication(), "购买成功");
                    adapter.notifyItemChanged(position, purchase);
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e) throws Exception {
                    ToastUtil.show(getApplication(), "购买失败");
                }
            });
        }
    }


}
